<template>
  <div>
    <a-list
      class="comment-list"
      itemLayout="horizontal"
      :dataSource="commentList"
    >
      <a-list-item slot="renderItem" slot-scope="item, index">
        <a-comment :author="item.nickName" :avatar="item.avatar">
          <template slot="actions">
            <span v-for="action in actions" @click="deleteComment(item.commentId,current)">{{action}}</span>
          </template>
          <p slot="content">{{item.replyContent}}</p>

          <a-tooltip slot="datetime" :title="item.createTime">
             <span>{{item.createTime}}</span>
           </a-tooltip>
        </a-comment>

      </a-list-item>
    </a-list>
    <a-pagination showQuickJumper :defaultCurrent="1" :defaultPageSize="10" :total="total" @change="onChange" v-show="total>0"/>
  </div>

</template>
<script>
  export default {
    name: 'commentList',
    props: {
      contentId: {
        type: String,
        default: ''
      },
      commentType: {
        type: Number,
        default: 0
      }
    },
    data() {
      return {
        current: 1,
        total: 1,
        actions:['删除'],
        commentList: []
      }
    },
    computed: {
      action: function() {
        let action = ''
        if (this.commentType == 1) {
          action = this.api.cmsComment.page
        }
        if (this.commentType == 2) {
          action = this.api.bbsComment.page
        }
        if (this.commentType == 3) {
          action = this.api.avmVideoComment.page
        }
        return action
      },
      delAction: function() {
        let action = ''
        if (this.commentType == 1) {
          action = this.api.cmsComment.delete
        }
        if (this.commentType == 2) {
          action = this.api.bbsComment.delete
        }
        if (this.commentType == 3) {
          action = this.api.avmVideoComment.delete
        }
        return action
      }
    },
    methods: {
      onChange(current) {
        this.current = current
        this.getCommentPageList(current)
      },
      getCommentPageList(page) {
        let data = {}
        if (this.commentType == 1 || this.commentType == 2) {
          data = { contentId: this.contentId, page: page }
        }
        if (this.commentType == 3) {
          data = { videoId: this.contentId, page: page }
        }
        this.http.get(this, {
          url: this.action,
          data: data,
          success(me, data) {
            console.log(data)
            me.total = Number(data.total)
            me.current = Number(data.current)
            me.commentList = data.records
          }
        })
      },
      deleteComment(commentId, current) {
        this.http.post(this, {
          url: this.delAction,
          data: {
            ids: commentId
          },
          success(me, data) {
            me.$nextTick(function() {
              me.getCommentPageList(current)
            })
          }
        })
      }
    },
    created() {
      this.getCommentPageList(this.current)
    }
  }
</script>
